package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.UserLoginFlowDao;
import com.wei.czz.framework.admin.entity.UserLoginFlowEntity;
import com.wei.czz.framework.admin.service.UserLoginFlowService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-06-10 11:07:45
 * className: UserLoginFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("userLoginFlowService")
@AllArgsConstructor
public class UserLoginFlowServiceImpl extends ServiceImpl<UserLoginFlowDao, UserLoginFlowEntity>
        implements UserLoginFlowService {

    private static final Logger log = LoggerFactory.getLogger(UserLoginFlowServiceImpl.class);

    @Override
    public void saveOrEdit(UserLoginFlowEntity userLoginFlow) {
        Long id = userLoginFlow.getId();

        if (Objects.isNull(id)) {

            int count = baseMapper.insert(userLoginFlow);
            log.info("插入用户登录流水完成。count={}", count);
        } else {

            int count = baseMapper.updateById(userLoginFlow);
            log.info("修改用户登录流水完成。count={}", count);
        }
    }

    @Override
    public PageDto<UserLoginFlowEntity> getPageList(Integer page) {
        // 获取请求用户主键
        Long optUserId = SecurityUtils.getUserId();

        // mp分页对象
        Page<UserLoginFlowEntity> mpPage = new Page<>(page, 10);

        LambdaQueryWrapper<UserLoginFlowEntity> userLoginFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLoginFlowLambdaQueryWrapper.eq(UserLoginFlowEntity::getUserId, optUserId)
                .eq(UserLoginFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 设置排序方式
        userLoginFlowLambdaQueryWrapper.orderByDesc(UserLoginFlowEntity::getCreateTime);
        // 分页查询
        baseMapper.selectPage(mpPage, userLoginFlowLambdaQueryWrapper);

        return new PageDto<>(mpPage);
    }

    @Override
    public List<UserLoginFlowEntity> findList(Long userId, Integer channel) {
        LambdaQueryWrapper<UserLoginFlowEntity> userLoginFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLoginFlowLambdaQueryWrapper.eq(UserLoginFlowEntity::getUserId, userId)
                .eq(UserLoginFlowEntity::getChannel, channel)
                .eq(UserLoginFlowEntity::getLogoutTime, CommonEnum.ZERO.getLongValue())
                .eq(UserLoginFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<UserLoginFlowEntity> userLoginFlowList = baseMapper.selectList(userLoginFlowLambdaQueryWrapper);
        log.info("查询用户登录流水完成。size={}", userLoginFlowList.size());
        return userLoginFlowList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<UserLoginFlowEntity> userLoginFlowList) {
        boolean bool = this.updateBatchById(userLoginFlowList);
        log.info("批量更新用户登录流水完成。bool={}", bool);
    }
}
